home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group93c.txt / 000126_icon-group-sender _Mon Dec 13 23:57:38 1993.msg < prev    next >
Internet Message Format  |  1994-02-02  |  4KB

  1. Received: by cheltenham.cs.arizona.edu; Thu, 16 Dec 1993 23:05:04 MST
  2. Date: 13 Dec 93 23:57:38 GMT
  3. From: dog.ee.lbl.gov!overload.lbl.gov!agate!howland.reston.ans.net!sol.ctr.columbia.edu!news.kei.com!world!ksr!tim@ucbvax.Berkeley.EDU  (Tim Peters)
  4. Organization: Kendall Square Research Corp.
  5. Subject: Re: Need help with (simple?) programming problem.
  6. Message-Id: <36955@ksr.com>
  7. References: <rfgCHyt15.Fpz@netcom.com>
  8. Sender: icon-group-request@cs.arizona.edu
  9. To: icon-group@cs.arizona.edu
  10. Status: R
  11. Errors-To: icon-group-errors@cs.arizona.edu
  12.  
  13. rfg@netcom.com (Ronald F. Guilmette) writes:
  14. >...
  15. >Assume that I have three strings, i.e. "red ", "green ", and "blue ". Now
  16. >I want to create a generator which will successively yield each unique
  17. >string which contains no more than one instance of each of the original
  18. >strings.  [should produce:]
  19. >
  20. >               ""
  21. >               "red "
  22. >               "green "
  23. >               "blue "
  24. >               "red green "
  25. >               "red blue "
  26. >               "green red "
  27. >               "green blue "
  28. >               "blue red "
  29. >               "blue green "
  30. >               "red green blue "
  31. >               "red blue green "
  32. >               "green red blue "
  33. >               "green blue red "
  34. >               "blue red green "
  35. >               "blue green red "
  36. >
  37. >[& order of generation is unimportant]
  38.  
  39. Two hints:
  40.  
  41. 1) A small library of simple combinatorial generators can be combined in
  42.    powerful ways.  For example, your problem can be viewed like this:
  43.    given a set of strings, generate each permutation of each subset of
  44.    that set.  Given a generator capable of generating subsets, and
  45.    another capable of generating permutations, you're basically done.
  46.  
  47. 2) Represent the string sets internally as lists of strings, delaying
  48.    conversion to a string representation until output.  The reason is
  49.    that lists are easy to index and pull apart etc, much easier than
  50.    reparsing strings all the time.
  51.  
  52. Here's a permutation generator (& no, the generators I'm posting aren't
  53. _obvious_ -- but they'll reward the effort of doping them out <wink>):
  54.  
  55. procedure perms( x ) # generate all permutations of list x
  56.     if *x <= 1 then return x
  57.     suspend x[1] <-> !x & push( perms(x[2:0]), x[1]  )
  58. end
  59.  
  60. And here's a subset generator:
  61.  
  62. procedure subsets( x )   # generate all subsets of list x
  63.    local head, tail
  64.    if *x = 0 then return []
  65.    head := x[1]
  66.    tail := x[2:0]
  67.    suspend subsets(tail) | push(subsets(tail), head)
  68. end
  69.  
  70. Now a function to paste a list of strings together:
  71.  
  72. procedure list2str( x )  # e.g., ["a", "bc", "d"] -> "abcd"
  73.    local answer
  74.    answer := ""
  75.    every answer ||:= !x
  76.    return answer
  77. end
  78.  
  79. Then, e.g.,
  80.  
  81. procedure main()
  82.    every write( list2str( perms( subsets( ["red ","green ","blue "] ) ) ) )
  83. end
  84.  
  85. writes
  86.  
  87. [an empty strng was printed on this line]
  88. blue 
  89. green 
  90. green blue 
  91. blue green 
  92. red 
  93. red blue 
  94. blue red 
  95. red green 
  96. green red 
  97. red green blue 
  98. red blue green 
  99. green red blue 
  100. green blue red 
  101. blue green red 
  102. blue red green 
  103.  
  104. Of course you can package the perms+subsets composition in another
  105. function, etc.  The key idea is just to build it out of simpler
  106. generators.
  107.  
  108. >P.S.  It probably makes no difference, but in the case of my *real* problem,
  109. >one or more of the "original strings" may not actually be strings at all.
  110. >They may instead be generators which produce various strings.
  111.  
  112. That may be a lot harder to deal with.  If possible, it would probably be
  113. best to materialize the sequences into lists, so that they can be fiddled
  114. via simple generators like the above.
  115.  
  116. combinatorially y'rs  - tim
  117.  
  118. Tim Peters   tim@ksr.com
  119. not speaking for Kendall Square Research Corp
  120.